{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "805e302c",
   "metadata": {},
   "source": [
    "# 项目立项审批流程优化\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bc40c99",
   "metadata": {},
   "source": [
    "## 1. 状态与动作空间定义\n",
    "\n",
    "状态包括：项目类型、投资规模、合规风险、可行性评分、立项阶段。\n",
    "动作包括：简化审批、标准审批、严格审批、补充评估、强化合规。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c813d635",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "状态空间大小: 243, 动作空间大小: 5\n"
     ]
    }
   ],
   "source": [
    "# 状态空间定义\n",
    "state_space = {\n",
    "    '项目类型': [0, 1, 2],  # 0:政府工程, 1:商业项目, 2:民生工程\n",
    "    '投资规模档': [0, 1, 2],  # 0:<1000万, 1:1000万-1亿, 2:>1亿\n",
    "    '合规风险档': [0, 1, 2],  # 0:低, 1:中, 2:高\n",
    "    '可行性评分档': [0, 1, 2],  # 0:低, 1:中, 2:高\n",
    "    '立项阶段进度': [0, 1, 2],  # 0:初期, 1:中期, 2:末期\n",
    "}\n",
    "\n",
    "# 动作空间定义\n",
    "actions = [\n",
    "    \"简化审批流程（快速通道）\",    # 0\n",
    "    \"标准审批流程（按常规步骤）\",  # 1\n",
    "    \"严格审批流程（多部门联审）\",  # 2\n",
    "    \"补充可行性评估（增加第三方论证）\", # 3\n",
    "    \"强化合规检查（提前补全资料）\"   # 4\n",
    "]\n",
    "\n",
    "num_states = 3 ** 5  # 每个维度3档，共5维\n",
    "num_actions = len(actions)\n",
    "print(f\"状态空间大小: {num_states}, 动作空间大小: {num_actions}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39773b57",
   "metadata": {},
   "source": [
    "## 2. Q-Learning算法实现\n",
    "\n",
    "初始化Q表，定义奖励函数，执行训练过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0f2f7fb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q-Learning训练完成！\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 状态编码与解码\n",
    "from itertools import product\n",
    "def encode_state(state):\n",
    "    idx = 0\n",
    "    for i, v in enumerate(state):\n",
    "        idx += v * (3 ** (len(state)-i-1))\n",
    "    return idx\n",
    "\n",
    "def decode_state(idx):\n",
    "    state = []\n",
    "    for i in range(5):\n",
    "        state.append(idx // (3 ** (4-i)))\n",
    "        idx = idx % (3 ** (4-i))\n",
    "    return state\n",
    "\n",
    "# Q表初始化\n",
    "Q = np.zeros((num_states, num_actions))\n",
    "\n",
    "# 奖励函数（示例：可根据实际业务调整）\n",
    "def reward_func(state, action):\n",
    "    # 高可行性+低风险+末期，简化审批奖励高\n",
    "    if state[2] == 0 and state[3] == 2 and state[4] == 2 and action == 0:\n",
    "        return 10\n",
    "    # 高风险+大规模+严格审批奖励高\n",
    "    if state[2] == 2 and state[1] == 2 and action == 2:\n",
    "        return 8\n",
    "    # 补充评估或强化合规可提升评分/降低风险\n",
    "    if action == 3 and state[3] < 2:\n",
    "        return 5\n",
    "    if action == 4 and state[2] > 0:\n",
    "        return 5\n",
    "    # 其他情况标准流程适中\n",
    "    if action == 1:\n",
    "        return 3\n",
    "    return 0\n",
    "\n",
    "# Q-Learning参数\n",
    "gamma = 0.9\n",
    "alpha = 0.1\n",
    "epsilon = 0.2\n",
    "num_episodes = 500\n",
    "\n",
    "# 训练过程\n",
    "for episode in range(num_episodes):\n",
    "    state = [np.random.choice([0,1,2]) for _ in range(5)]\n",
    "    for step in range(10):\n",
    "        s_idx = encode_state(state)\n",
    "        if np.random.rand() < epsilon:\n",
    "            action = np.random.choice(num_actions)\n",
    "        else:\n",
    "            action = np.argmax(Q[s_idx])\n",
    "        r = reward_func(state, action)\n",
    "        next_state = state.copy()\n",
    "        # 简单模拟：部分动作可改变状态\n",
    "        if action == 3 and next_state[3] < 2:\n",
    "            next_state[3] += 1  # 可行性评分提升\n",
    "        if action == 4 and next_state[2] > 0:\n",
    "            next_state[2] -= 1  # 合规风险降低\n",
    "        ns_idx = encode_state(next_state)\n",
    "        Q[s_idx, action] += alpha * (r + gamma * np.max(Q[ns_idx]) - Q[s_idx, action])\n",
    "        state = next_state\n",
    "print(\"Q-Learning训练完成！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "444eaa5b",
   "metadata": {},
   "source": [
    "## 3. 策略查询与可视化\n",
    "\n",
    "给定某一状态，查询最优动作，并展示部分策略。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c2cd138c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "示例状态: [2, 2, 2, 0, 0]\n",
      "推荐动作: 严格审批流程（多部门联审）\n",
      "状态: [0, 0, 2, 2, 2] -> 推荐动作: 标准审批流程（按常规步骤）\n",
      "状态: [0, 1, 2, 2, 2] -> 推荐动作: 简化审批流程（快速通道）\n",
      "状态: [0, 2, 2, 2, 2] -> 推荐动作: 简化审批流程（快速通道）\n",
      "状态: [0, 3, 2, 2, 2] -> 推荐动作: 简化审批流程（快速通道）\n",
      "状态: [0, 4, 2, 2, 2] -> 推荐动作: 标准审批流程（按常规步骤）\n"
     ]
    }
   ],
   "source": [
    "# 查询某一状态的最优动作\n",
    "example_state = [2, 2, 2, 0, 0]  # 民生工程, >1亿, 高风险, 低可行性, 初期\n",
    "s_idx = encode_state(example_state)\n",
    "best_action = np.argmax(Q[s_idx])\n",
    "print(f\"示例状态: {example_state}\")\n",
    "print(f\"推荐动作: {actions[best_action]}\")\n",
    "\n",
    "# 展示部分策略\n",
    "for i in range(5):\n",
    "    state = [0, i, 2, 2, 2]  # 政府工程, 投资规模变化, 高风险, 高可行性, 末期\n",
    "    s_idx = encode_state(state)\n",
    "    best_action = np.argmax(Q[s_idx])\n",
    "    print(f\"状态: {state} -> 推荐动作: {actions[best_action]}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
